<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="500px" :height=100 destroyOnClose>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { refundFormSchema } from './order.data';
import { refundOrder } from '../refund/refund.api';
import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();
// 声明Emits
const emit = defineEmits(['register', 'success']);
const record = ref<any>({})
//表单配置
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  schemas: refundFormSchema,
  labelWidth: 100,
  showActionButtonGroup: false
});
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false });
  //表单赋值
  // await setFieldsValue({
  //   ...data.record,
  // });
  console.log("data.record: ", data);
  
  record.value = data.record
});
//设置标题
const title = computed(() => '退款');
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();
    setModalProps({ confirmLoading: true });
    await refundOrder({
      payOrderNo: record.value.payOrderNo, 
      refundReason: values.refundReason,
      refundAmount: values.refundAmount,
      currency: 'CNY',
      mchNo: record.value.mchNo,
      appId: record.value.appId,
      mchRefundNo: 'MR' + new Date().getTime(),
    })
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
    createMessage.success(`${unref(title)}成功`)
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
